package Concurrency2.Chapter05_Lock;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Instruction：测试非公平锁的抢锁与获取到锁的顺序
 * Author：@author MaLi
 */
public class T06_NoFairLock {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        ArrayList<Thread> threads = new ArrayList<>(8);
        for (int i = 0; i < 8; i++) {
            Thread thread = new Thread(() -> {
                String tName = Thread.currentThread().getName();
                System.err.println(tName + "  start");
                lock.lock();
                try {
                    System.out.println(tName + " ---> 获取到锁");
                } finally {
                    lock.unlock();
                }
            });
            threads.add(thread);
        }

        for (Thread thread : threads) {
            thread.start();
        }
    }
}
